package com.promis.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;

public class ColorUtils {

	@RequiresApi(api = Build.VERSION_CODES.M)
	public static ColorStateList getColorStateList(Context context, int resId) {
		final int version = Build.VERSION.SDK_INT;
		if (version >= 23) {
			return ContextCompat.getColorStateList(context, resId);
		} else {
			return context.getResources().getColorStateList(resId);
		}
	}
	
	public static ColorStateList getRadioColorStateList(Context context, int normalResId, int checkedResId) {  
        return getColorStateList(context, android.R.attr.state_checked, normalResId, checkedResId);  
	}
	
	public static ColorStateList getColorStateList(Context context, int state, int normalResId, int checkedResId) {  
        int[] colors = new int[] {getColor(context, normalResId), getColor(context, checkedResId)};  
        int[][] states = new int[2][];
        states[0] = new int[] {-state};
        states[1] = new int[] {state};  
        ColorStateList colorList = new ColorStateList(states, colors);  
        return colorList;  
	}
	
	public static int getColor(Context context, int resId) {
		final int version = Build.VERSION.SDK_INT;
		if (version >= 23) {
			return ContextCompat.getColor(context, resId);
		} else {
			return context.getResources().getColor(resId);
		}
	}
}
